package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Package;
import com.woniu.service.ITeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.QuerySearchTeamLabelVo;
import com.woniu.vo.QuerySearchTeamVo;
import com.woniu.vo.QueryUserVo;
import com.woniu.vo.TeamLabelVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rsw
 * @since 2022-10-11
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Resource
    private TeamMapper teamMapper;
    @Resource
    private TeamDoctorMapper teamDoctorMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private TeamPackageMapper teamPackageMapper;
    @Resource
    private PackageMapper packageMapper;

    @Override
    public PageResult searchByPage(String currentPage, String pageSize, QuerySearchTeamVo searchTeamVo) {

        QuerySearchTeamLabelVo querySearchTeamLabelVo = new QuerySearchTeamLabelVo();

        QueryWrapper<Team> wrapper=new QueryWrapper();


        int currentPag = Integer.parseInt(currentPage);
        int pageSiz = Integer.parseInt(pageSize);

        Page<Team> teamPage = new Page<>(currentPag, pageSiz);
        PageResult pageResult = new PageResult();

        //传过来的对象不是空的
        if (searchTeamVo!=null){

            //团队名字不为空按团队名搜索
            if (searchTeamVo.getName()!=null){
                wrapper.like("name",searchTeamVo.getName());
            }
            //团队id
            if (searchTeamVo.getId()!=null){
                wrapper.eq("id",searchTeamVo.getId());
            }
            //团队长
            if (searchTeamVo.getDcName()!=null){
                wrapper.eq("dc_name",searchTeamVo.getDcName());
            }
            if (searchTeamVo.getJgName()!=null){
                wrapper.eq("jg_name",searchTeamVo.getJgName());
            }
            IPage<Team> teamIPage = teamMapper.selectPage(teamPage, wrapper);
            pageResult.setTotal(teamIPage.getTotal());
            pageResult.setData(teamIPage.getRecords());

            System.out.println(wrapper+"warpper++++++++++++++++++++++");

            return pageResult;
        }
        IPage<Team> teamIPage = teamMapper.selectPage(teamPage, null);
        log.debug(String.valueOf(teamIPage.getTotal()));

        pageResult.setTotal(teamIPage.getTotal());
        pageResult.setData(teamIPage.getRecords());




        return null;
    }

    @Override
    public List<QueryUserVo> getPgByteamId(int id) {

        List<QueryUserVo> pgList = teamMapper.selectPackageIdByTeamId(id);

        return pgList;
    }

    @Override
    public List<String> getLabelByteamId(int id) {

        List<String> labelList = teamMapper.selectLabelIdByTeamId(id);


        return labelList;
    }

    /**
     * 根据团队id查找所有医生
     * @param id
     * @return
     */
    @Override
    public List<Doctor> searchDoctorByTeamId(Long id) {
        QueryWrapper<TeamDoctor> wrapper = new QueryWrapper<>();
        List<Doctor> doctorList = new ArrayList<>();
        wrapper.eq("team_id",id);
        List<TeamDoctor> teamDoctorList = teamDoctorMapper.selectList(wrapper);
        for (TeamDoctor teamDoctor : teamDoctorList) {
            Long doctorId = teamDoctor.getDoctorId();
            Doctor doctor = doctorMapper.selectById(doctorId);
            doctorList.add(doctor);
        }
        return doctorList;
    }

    /**
     * 根据团队id查找所有服务包2
     * @param id
     * @return
     */
    @Override
    public List<Package> getPgByteamId2(Long id) {
        QueryWrapper<TeamPackage> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id",id);
        List<Package> packageList = new ArrayList<>();
        List<TeamPackage> teamPackages = teamPackageMapper.selectList(wrapper);
        for (TeamPackage teamPackage : teamPackages) {
            Long packageId = teamPackage.getPackageId();
            Package aPackage = packageMapper.selectById(packageId);
            packageList.add(aPackage);
        }
        return packageList;
    }
}
